package org.kk.ds.hash;

import org.kk.ds.linked.DoubleLinked;


/**
 * @author kk
 * @Date 2021/2/5
 */
public class HashTableDemo {
    private DoubleLinked[] items;
    private int length;
    private int modSize = 1 << 5;

    public HashTableDemo(){
        items = new DoubleLinked[modSize];
        for (int i = 0; i < modSize; i++) {
            items[i] = new DoubleLinked();
        }
    }
    public int size(){
        return length;
    }
    public boolean isEmpty(){
        return length == 0;
    }
    public void put(Integer key){
        int hash = hashCode(key);
        putVal(hash, key);
        length++;
    }
    private void putVal(int hash, Integer key){
        items[hash].addAtTail(key);
    }
    public void remove(Integer key){
        int hash = hashCode(key);
        items[hash].removeKey(key);
        length--;
    }
    private final int hashCode(Object key){
        return key == null ? 0 : key.hashCode() & (2 ^ modSize -1);
    }
}
